package hk.hku.hooku.servlet;

import hk.hku.hooku.db.EventDB;
import hk.hku.hooku.pojo.EventChoice;
import hk.hku.hooku.pojo.EventResult;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class UpdateDesicionServlet
 */
public class UpdateDesicionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateDesicionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url=request.getParameter("url");
		if(url!=null){
			String note=request.getParameter("note");
			String choiceIds=request.getParameter("choices");
			String[] ids=choiceIds.split(",");
			ArrayList<EventChoice> choices=new ArrayList<EventChoice>();
			for(String idString:ids){
				int id=Integer.valueOf(idString);
				EventChoice choice=new EventChoice();
				choice.setId(id);
				choices.add(choice);
			}
			EventResult result=new EventResult(choices,note);
			boolean isSuccess=EventDB.UpdateDecision(url, result);
			PrintWriter writer=response.getWriter();
			if(isSuccess){
				writer.write("true");
			}else{
				writer.write("false");
			}
		}
	}

}
